<?php



function is_logged_in($to="",$type = array(1,2)) 
{
	$user = $_SESSION['user_id'];
	
	if($to == "")
	{
		$to = "user/login/";
	}
	else{
		$to = $to;
	}
	
	
	
    if ($user <= 0) 
	{ 
		redirect($to); 
	}
	else{
		
		$u_type = $_SESSION['user_type'];
		
		if(!in_array($u_type,$type))
		redirect($to); 
	}
}

function getUser_type()
{
	return  $_SESSION['user_type'];
}

function isMobile()
{
	$ua = $_SERVER['HTTP_USER_AGENT'];
		if (
			 stristr($ua, "Windows CE") or
			 stristr($ua, "AvantGo") or
			 stristr($ua,"Mazingo") or
			 stristr($ua, "Mobile") or
			 stristr($ua, "T68") or
			 stristr($ua,"Syncalot") or
			 stristr($ua, "Blazer") or
			 stristr($ua, "iPhone") or 
			 stristr($ua, "Android") or
			  stristr($ua, "Mobile")
			 ) 
		 {
			 return TRUE;
		 }
		 else{
		 	return FALSE;
		 }
}

function gen_time_dropdown($type='hr' , $selected= "")
{
	$timeOut = "";
	$timeHrStr = array("00"
					,"01"
					,"02"
					,"03"
					,"04"
					,"05"
					,"06"
					,"07"
					,"08"
					,"09"
					,"10"
					,"11"
					,"12"
					,"13"
					,"14"
					,"15"
					,"16"
					,"17"
					,"18"
					,"19"
					,"20"
					,"22"
					,"23"
					,"24"
				  );
$timeMinStr = array("00"
					,"30"

				  );	
	$select = "";
	$cur_hr = date("hi");
	$sel = "";
	if($type == 'hr')
	{
		$out = $timeHrStr;
	}
	else{
		$out = $timeMinStr	;
	}
	foreach($out as $t)
	{
//		$diff = $cur_hr . " - " . (float)($t) . " = " . abs($cur_hr - (float)($t) );
//		echo $diff . "<br />";
//		if( $diff  <= 30 )
//		{
//			$select = " selected ";	
//		}
//		$timeOut .= "<option $select value='". $t ."'>" . $t ."</option>\n";	
//		$select = "";
		if($t == $selected)
			$sel =" selected ";
		$timeOut .= "<option $select value='". substr($t,0,2) .substr($t,2,2) ."' '" . $sel ."'>" . substr($t,0,2) . substr($t,2,2) ."</option>\n";	
		$sel = "";
	}
	
	return $timeOut;
}

function gen_mobile_date_dropdown()
{
	$start = "2011-01-00";//date("2010-01-01");//date("Y-m-d", strtotime("2010-01-01"));

	$end_range =  365;
	$start_range = 0;
	$out = "";
	$select = "";
	
	do
	{
		if($start == date("Y-m-d"))
			$select = " selected ";
		
		$cDate = date("Y-m-d", strtotime($start . " +1 day"));
		$out .=  "<option $select value='". $cDate ."'>" . date("D d-m-Y  ", strtotime($cDate)) ."</option>\n";
		$start = $cDate;

		
		$start_range++;
		$select = "";
	}while($end_range > $start_range);
	
	return $out;//date("Y-m-d", strtotime("+1 day"));
}


function format_hours_logged($str, $pad_hrs= "")
{

	$hr = ((float)$str)/10000;
	$t = explode(".",$hr);
	if(count($t) == 2)
	{
		return (int)$t[0] + (float)(($t[1]/60)*10);
	}
	else{
		return (int)$t[0];
	}

	


}

function format_job_time($t)
{
	if($t == "0000-00-00 00:00:00")
	{
		return " ";
	}
	else{
		return $t;
	}
	
}


function gen_interval($s = "")
{
	$out = "";
	$select = "";
	for($i = 1; $i<=60; $i++)
	{
		if($s == $i)
		{
			$select = "  selected  ";
		}
		else{
			$select = "";
		}
			$out .=  "<option $select  value='". $i ."'>" . $i ."</option>\n";
		
	
	}
	
	return $out;
}


function get_SYS_WELCOME()
{
	
	$ci = &get_instance();
	$ci->load->model("model_lu");
	return $ci->model_lu->get_SYS_MSG();
}


 function rel_time($from, $to = null)
 {
  $to = (($to === null) ? (time()) : ($to));
  $to = ((is_int($to)) ? ($to) : (strtotime($to)));
  $from = ((is_int($from)) ? ($from) : (strtotime($from)));
	$output= "";
  $units = array
  (
   "year"   => 29030400, // seconds in a year   (12 months)
   "month"  => 2419200,  // seconds in a month  (4 weeks)
   "week"   => 604800,   // seconds in a week   (7 days)
   "day"    => 86400,    // seconds in a day    (24 hours)
   "hr"   => 3600,     // seconds in an hour  (60 minutes)
   "min" => 60,       // seconds in a minute (60 seconds)
   "s" => 1         // 1 second
  );

  $diff = abs($from - $to);
  $suffix = "";//(($from > $to) ? ("from now") : ("ago"));

  foreach($units as $unit => $mult)
   if($diff >= $mult)
   {
    $and = (($mult != 1) ? ("") : ("and "));
    $output .= ", ".$and.intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : (""));
    $diff -= intval($diff / $mult) * $mult;
   }
  $output .= " ".$suffix;
  $output = substr($output, strlen(", "));

  return $output;
 }
